#!/bin/bash
#通过inet判断,修改网卡配置文件：禁用网卡onboot=no

#sh
SHPATH=`readlink -f $0`
SHDIR=`dirname $SHPATH`
SHNAME="$0"

#dir
LOG_DIR=$SHDIR/logs/$SHNAME
TMP_DIR=$SHDIR/temp/$SHNAME
OUT_DIR=$SHDIR/out/$SHNAME
mkdir -p $LOG_DIR $TMP_DIR $OUT_DIR


>$OUT_DIR/not_connecte_dev.txt
>$OUT_DIR/all_dev.txt
#
ip a |grep -v "^$" >/dev/null 2>&1
if [ $? -eq 0 ];then IPCMD='ip a';else IPCMD='ifconfig';fi
$IPCMD > $TMP_DIR/info.txt

#
WC=`cat $TMP_DIR/info.txt|wc -l`
seq $WC|xargs -n1 >$TMP_DIR/ln.txt
paste $TMP_DIR/info.txt $TMP_DIR/ln.txt|grep mtu|awk '{print $NF}' >$TMP_DIR/info_ln.txt
echo '$' >> $TMP_DIR/info_ln.txt

WC2=`cat $TMP_DIR/info_ln.txt|wc -l`
for i in `seq  $WC2`
do
if [ $i -eq $WC2 ];then continue;fi
LINE_NUM1=`sed -n ${i}p $TMP_DIR/info_ln.txt`
LINE_NUM2=`sed -n $((i+1))p $TMP_DIR/info_ln.txt`
sed -n "$LINE_NUM1,$LINE_NUM2"p $TMP_DIR/info.txt >$OUT_DIR/netdev.${i}.txt
cat $OUT_DIR/netdev.${i}.txt|head -1|awk -F ':' '{print $2}' >>$OUT_DIR/all_dev.txt
cat $OUT_DIR/netdev.${i}.txt|grep 'inet' >/dev/null
if [ ! $? -eq 0 ];then 	
	INET_NAME=`cat $OUT_DIR/netdev.${i}.txt|head -1|awk -F ':' '{print $2}'|tr -d " "`
	echo $INET_NAME >$OUT_DIR/not_connecte_dev.txt
	if [ -f  "/etc/sysconfig/network-scripts/ifcfg-$INET_NAME" ];then
		if [ $? -eq 0 ];then 
			sed -i '/onboot=/ s/^/#/' /etc/sysconfig/network-scripts/ifcfg-$INET_NAME  
			sed -i '/ONBOOT=/ s/^/#/' /etc/sysconfig/network-scripts/ifcfg-$INET_NAME
			sed -i '/onboot=/a onboot=no' /etc/sysconfig/network-scripts/ifcfg-$INET_NAME 
			sed -i '/ONBOOT=/a ONBOOT=no' /etc/sysconfig/network-scripts/ifcfg-$INET_NAME 
			echo "禁用网卡：$INET_NAME" 
		fi
	fi
fi
done














